package com.huanghuai.retail.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huanghuai.retail.common.R;
import com.huanghuai.retail.dto.InformationDto;
import com.huanghuai.retail.entity.*;
import com.huanghuai.retail.mapper.InformationMapper;
import com.huanghuai.retail.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class InformationServiceImpl extends ServiceImpl<InformationMapper, PInformation> implements InformationService {
    @Autowired
    private InformationService informationService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private FactoryService factoryService;

    @Autowired
    private BrandTypeService brandTypeService;

    @Autowired
    private SInventoryService sInventoryService;

    @Autowired
    private StorageService storageService;


    /**
     * 查询商品信息，并封装到dto中
     * @return
     */
    @Override
    public List<InformationDto> findInformationDto() {
        List<PInformation> informationList = informationService.list();
        if (informationList == null)
            return null;
        List<InformationDto> informationDtoList = new ArrayList<>();

        for(int i = 0; i < informationList.size(); i ++){
            PInformation information = informationList.get(i);

            InformationDto informationDto = informationService.disposeDto(information);

            informationDtoList.add(informationDto);
        }

        return informationDtoList;
    }

    @Override
    public InformationDto disposeDto(PInformation information) {
        InformationDto informationDto = new InformationDto();
        //copy基本信息
        BeanUtils.copyProperties(information, informationDto);

        //处理商品类型信息
        Long typeId = information.getTypeId();
        PType pType = typeService.getById(typeId);
        informationDto.setTypeName(pType.getNameTwo());

        //处理工厂信息
        Long factoryId = information.getFactoryId();
        Factory factory = factoryService.getById(factoryId);
        informationDto.setFactoryName(factory.getName());

        //处理品牌信息
        Long brandId = information.getBrandId();
        PBrand brand = brandService.getById(brandId);
        informationDto.setBrandName(brand.getName());

        return informationDto;
    }

    /**
     * 根据商品信息维护品牌类别表
     * @param information
     */
    @Override
    public void maintenance(PInformation information) {
        Long brandId = information.getBrandId();
        Long typeId = information.getTypeId();
        PBrandType pBrandType = new PBrandType();
        pBrandType.setBrandId(brandId);
        pBrandType.setTypeId(typeId);

        LambdaQueryWrapper<PBrandType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PBrandType::getBrandId, brandId);
        lambdaQueryWrapper.eq(PBrandType::getTypeId, typeId);
        PBrandType brandType = brandTypeService.getOne(lambdaQueryWrapper);
        //判断是否以及存在，如果不存在，则新增
        if (brandType == null)
            brandTypeService.save(pBrandType);
    }

    @Override
    public boolean pdInformation(PInformation information) {
        if (information.getName() == null || information.getTypeId() == null ||
            information.getFactoryId() == null || information.getBrandId() == null ||
            information.getWrap() == null || information.getPrice() == null)
            return false;

        if(factoryService.getById(information.getFactoryId()) == null ||
            brandService.getById(information.getFactoryId()) == null ||
            typeService.getById(information.getTypeId()) == null)
            return false;

        return true;
    }

    @Override
    public void pdDelBrandType(PInformation information) {
        information = informationService.getById(information.getId());
        Long brandId = information.getBrandId();
        Long typeId = information.getTypeId();
        LambdaQueryWrapper<PInformation> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PInformation::getTypeId, typeId);
        lambdaQueryWrapper.eq(PInformation::getBrandId, brandId);
        List<PInformation> list = informationService.list(lambdaQueryWrapper);
        if (list.size() > 1)
            return;

        //删除
        LambdaQueryWrapper<PBrandType> brandTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        brandTypeLambdaQueryWrapper.eq(PBrandType::getTypeId, typeId);
        brandTypeLambdaQueryWrapper.eq(PBrandType::getBrandId, brandId);
        brandTypeService.remove(brandTypeLambdaQueryWrapper);
    }

    public R<Page> getShopsPage( Integer page,Integer pageSize,List shopId,List shopTypeId, List shopName, List specs,List storageId) {

        LambdaQueryWrapper<SInventory> queryWrapper = new LambdaQueryWrapper<>();
        //处理仓库id
        if (storageId == null||storageId.size()==0) {
            queryWrapper.select(SInventory::getProductId);
        }
        else {
            queryWrapper.in(SInventory::getStorageId, storageId).select(SInventory::getProductId);
        }

        Page<InformationDto> informationDtoPage  = getpInformationPage(page, pageSize, shopId, shopTypeId, shopName, specs,storageId, queryWrapper);
        return R.success(informationDtoPage);

    }

    private Page<InformationDto> getpInformationPage(Integer page, Integer pageSize, List<Integer> shopId, List<Integer> shopTypeId, List<String> shopName, List<String> specs, List<Integer> storageId ,LambdaQueryWrapper<SInventory> queryWrapper) {
        List<SInventory> sInventoryList;
        sInventoryList = sInventoryService.list(queryWrapper);
        List<Long> shopIdList = null;
        if(sInventoryList.size()==0||sInventoryList==null||sInventoryList.equals(""))
            return new Page<InformationDto>(page,pageSize);
        shopIdList = sInventoryList.stream().map(SInventory::getProductId).collect(Collectors.toList());
        LambdaQueryWrapper<PInformation> informationQueryWrapper = new LambdaQueryWrapper<>();
        if(shopIdList != null && !shopIdList.equals("")){
            informationQueryWrapper.in(PInformation::getId, shopIdList);
        }
        //处理货品类别
        if(shopTypeId != null&&shopTypeId.size()>0 && !shopTypeId.equals("")){
            informationQueryWrapper.in(PInformation::getTypeId, shopTypeId);
        }
        //处理货品编码
        if(shopId != null &&shopId.size()>0 && !shopId.equals("")){
            informationQueryWrapper.in(PInformation::getId, shopId);
        }
        //处理货品名称
        if(shopName != null && shopName.size()>0 && !shopName.equals("")){
            informationQueryWrapper.in(PInformation::getName, shopName);
        }
        //处理规格型号
        if(specs != null &&specs.size()>0 && !specs.equals("")){
            informationQueryWrapper.in(PInformation::getSpecs, specs);
        }
        //分页操作
        Page<InformationDto> informationDtoPage = new Page<>(page,pageSize);
        List<InformationDto>  dtoList = new ArrayList<>();
        List<PInformation> pInformations = informationService.list(informationQueryWrapper);
        System.out.println("pInformations = " + pInformations.size());
        int total = 0;
        Boolean st = false;
        for (PInformation pInformation : pInformations) {
            Long id = pInformation.getId();
            List<SInventory> sInventoryList2 = null;
            if (storageId == null || storageId.size() == 0)
                sInventoryList2 = sInventoryService.query().eq("product_id", id).list();
            else
                sInventoryList2 = sInventoryService.query().eq("product_id", id).in("storage_id",storageId).list();
            for (SInventory sInventory : sInventoryList2) {
                total ++;
                if (total <= (page-1)*pageSize)
                    continue;
                if (total > page*pageSize)
                {
                    total --;
                    st = true;
                    break;
                }
                InformationDto informationDto = new InformationDto();
                BeanUtils.copyProperties(pInformation, informationDto);
                Long typeId = pInformation.getTypeId();
                PType pType = typeService.getById(typeId);
                informationDto.setTypeName(pType.getNameTwo());
                informationDto.setInventoryNumber(sInventory.getNumber());
                informationDto.setMinRate(sInventory.getMinRate());
                Storage storage = storageService.getById(sInventory.getStorageId());
                informationDto.setStorageName(storage.getStorageName());
                dtoList.add(informationDto);
            }
            if (st)
                break;
        }
        informationDtoPage.setRecords(dtoList);
        total = total-(page-1)*pageSize;
        if (total > 0)
            informationDtoPage.setTotal(total);
        else
            informationDtoPage.setTotal(0);
        return informationDtoPage;
    }

    @Override
    public List<PInformation> getIdByTypeId(Long typeId) {
        if(typeId == null){
            return null;
        }
        LambdaQueryWrapper<PInformation> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.select(PInformation::getId).eq(PInformation::getTypeId, typeId);
        List<PInformation> productId = this.list(queryWrapper);
        return productId;
    }
}
